package com.campusbackend.controller;

import com.campusbackend.entity.Result;
import com.campusbackend.entity.vo.AdminTeamListVO;
import com.campusbackend.entity.vo.AdminTeamDetailVO;
import com.campusbackend.service.TeamsService;
import com.campusbackend.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 管理端队伍管理控制器
 * </p>
 *
 * @author yezi
 * @since 2025-08-01
 */
@Slf4j
@RestController
@RequestMapping("/adminTeam")
@Tag(name = "管理端队伍管理", description = "管理端队伍管理相关接口")
public class AdminTeamController {

    @Autowired
    private TeamsService teamsService;

    /**
     * 获取所有队伍列表
     * @return 队伍列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取所有队伍列表", description = "管理端获取所有队伍的列表信息")
    public Result<List<AdminTeamListVO>> getTeamList() {
        try {
            log.info("管理端获取队伍列表");
            List<AdminTeamListVO> teamList = teamsService.getAdminTeamList();
            return ResultUtil.success(teamList);
        } catch (Exception e) {
            log.error("获取队伍列表失败", e);
            return ResultUtil.fail("获取队伍列表失败，请稍后重试");
        }
    }

    /**
     * 获取队伍详细信息
     * @param teamId 队伍ID
     * @return 队伍详细信息
     */
    @GetMapping("/detail/{teamId}")
    @Operation(summary = "获取队伍详细信息", description = "管理端获取指定队伍的详细信息，包括队员、指导老师、入驻专家等")
    public Result<AdminTeamDetailVO> getTeamDetail(
            @Parameter(description = "队伍ID", required = true) 
            @PathVariable Long teamId) {
        try {
            log.info("管理端获取队伍详情 - teamId: {}", teamId);
            
            if (teamId == null || teamId <= 0) {
                return ResultUtil.fail("队伍ID不能为空");
            }
            
            AdminTeamDetailVO teamDetail = teamsService.getAdminTeamDetail(teamId);
            if (teamDetail == null) {
                return ResultUtil.fail("队伍不存在或已解散");
            }
            
            return ResultUtil.success(teamDetail);
        } catch (Exception e) {
            log.error("获取队伍详情失败 - teamId: {}", teamId, e);
            return ResultUtil.fail("获取队伍详情失败，请稍后重试");
        }
    }

    /**
     * 搜索队伍
     * @param keyword 搜索关键词（队伍名称或编号）
     * @return 匹配的队伍列表
     */
    @GetMapping("/search")
    @Operation(summary = "搜索队伍", description = "管理端根据队伍名称或编号搜索队伍")
    public Result<List<AdminTeamListVO>> searchTeams(
            @Parameter(description = "搜索关键词（队伍名称或编号）", required = true)
            @RequestParam String keyword) {
        try {
            log.info("管理端搜索队伍 - keyword: {}", keyword);
            
            if (keyword == null || keyword.trim().isEmpty()) {
                return ResultUtil.fail("搜索关键词不能为空");
            }
            
            List<AdminTeamListVO> teamList = teamsService.adminSearchTeams(keyword);
            return ResultUtil.success(teamList);
        } catch (Exception e) {
            log.error("搜索队伍失败 - keyword: {}", keyword, e);
            return ResultUtil.fail("搜索队伍失败，请稍后重试");
        }
    }
}